package com.greatwall.hip.cms.controller;

import cn.hutool.core.util.NumberUtil;
import com.greatwall.hip.cms.entity.MedicalServiceData;
import com.greatwall.hip.cms.model.Req;
import com.greatwall.hip.cms.model.Result;
import com.greatwall.hip.cms.service.MedicalServiceDataService;
import com.greatwall.hip.cms.vo.*;
import com.greatwall.hip.cms.vo.extend.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TianLei
 * @version V1.0
 */
@RestController
@RequestMapping("medicalServiceData")
@Api(tags = "基本医疗模块", value = "医疗服务&药品收入")
public class MedicalServiceDataController extends BaseController{

    @Resource
    private MedicalServiceDataService medicalServiceDataService;

    @ApiOperation("查询药品收入汇总统计表")
    @PostMapping("/analyseDragIncome")
    public Result<DragIncomeAnalyseVO> analyseDragIncome(@RequestBody Req req) {
        checkDateReq(req);
        MedicalServiceData medicalServiceData = medicalServiceDataService.queryStatisticsData(req);
        if( null == medicalServiceData) {
            medicalServiceData = new MedicalServiceData();
        }
        DragIncomeAnalyseVO vo = new DragIncomeAnalyseVO();
        vo.setInpatientDragIncome(medicalServiceData.getInpatientDragIncome());
        vo.setSpecOutpatientDragIncome(medicalServiceData.getSpecOutpatientDragIncome());
        vo.setTotalIncome(medicalServiceData.getTotalIncome());
        vo.setSpecOutpatientAndInpatientDragIncome(medicalServiceData.getInpatientDragIncome() + medicalServiceData.getSpecOutpatientDragIncome());
        vo.setSpecOutpatientAndInpatientDragIncomePercent(NumberUtil.parseDouble(divScale(vo.getSpecOutpatientAndInpatientDragIncome(), medicalServiceData.getTotalIncome(), 2)));
        vo.setInpatientDragIncomePercent(NumberUtil.parseDouble(divScale(medicalServiceData.getInpatientDragIncome(), medicalServiceData.getTotalIncome(), 2)));
        vo.setSpecOutpatientDragIncomePercent(NumberUtil.parseDouble(divScale(medicalServiceData.getSpecOutpatientDragIncome(), medicalServiceData.getTotalIncome(), 2)));
        return Result.success(vo);
    }

    @ApiOperation("基本医疗服务")
    @PostMapping("/analyseMedicalService")
    public Result<MedicalServiceAnalyseVO> analyseMedicalService(@RequestBody Req req) {
        checkDateReq(req);
        MedicalServiceData medicalServiceData = medicalServiceDataService.queryStatisticsData(req);
        if( null == medicalServiceData) {
            medicalServiceData = new MedicalServiceData();
        }
        MedicalServiceAnalyseVO vo = new MedicalServiceAnalyseVO();
        vo.setCheckPerson(medicalServiceData.getCheckPerson());
        vo.setLeaveHospitalPerson(medicalServiceData.getLeaveHospitalPerson());
        vo.setTotalIncome(medicalServiceData.getTotalIncome());
        vo.setAllOutpatientPerson(medicalServiceData.getOutpatientPerson() + medicalServiceData.getSpecOutpatientPerson());

        Double allDragIncome = NumberUtil.add(medicalServiceData.getInpatientDragIncome(), medicalServiceData.getSpecOutpatientDragIncome());
        vo.setSpecOutpatientAndInpatientDragIncome(allDragIncome);
        vo.setDragPercent(NumberUtil.parseDouble(divScale(allDragIncome, medicalServiceData.getTotalIncome(),2)));
        Double allOutpatientIncome = NumberUtil.add(medicalServiceData.getOutpatientIncome(), medicalServiceData.getSpecOutpatientIncome());
        vo.setOutpatientCostAvg(NumberUtil.parseDouble(divScale(allOutpatientIncome, vo.getAllOutpatientPerson(), 2)));
        return Result.success(vo);
    }

    @ApiOperation("查询药品收入分布表")
    @PostMapping("/dragIncomeDistributed")
    public Result<DragIncomeDistributedExtendVO> dragIncomeDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        DragIncomeDistributedExtendVO vo = new DragIncomeDistributedExtendVO();
        vo.setDate(date);
        List<Double> inpatientDragIncome = new ArrayList<>();
        List<Double> outpatientDragIncome = new ArrayList<>();
        List<Double> specOutpatientDragIncome = new ArrayList<>();
        List<Double> allDragIncome = new ArrayList<>();
        List<DragIncomeDistributedVO> datas = medicalServiceDataService.dragIncomeDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(datas == null || datas.isEmpty()) {
                inpatientDragIncome.add(0.0);
                outpatientDragIncome.add(0.0);
                specOutpatientDragIncome.add(0.0);
                allDragIncome.add(0.0);
            } else {
                double inpatientDragIncomeCnt = 0.0;
                double outpatientDragIncomeCnt = 0.0;
                double specOutpatientDragIncomeCnt = 0.0;
                double allDragIncomeCnt = 0.0;
                for (DragIncomeDistributedVO dragIncomeDistributedVO : datas) {
                    if (StringUtils.equals(date.get(i), dragIncomeDistributedVO.getDatePoint())) {
                        inpatientDragIncomeCnt = dragIncomeDistributedVO.getInpatientDragIncome();
                        outpatientDragIncomeCnt = dragIncomeDistributedVO.getOutpatientDragIncome();
                        specOutpatientDragIncomeCnt = dragIncomeDistributedVO.getSpecOutpatientDragIncome();
                        allDragIncomeCnt = dragIncomeDistributedVO.getInpatientDragIncome() + dragIncomeDistributedVO.getOutpatientDragIncome() +
                                dragIncomeDistributedVO.getSpecOutpatientDragIncome();
                    }
                }
                inpatientDragIncome.add(inpatientDragIncomeCnt);
                outpatientDragIncome.add(outpatientDragIncomeCnt);
                specOutpatientDragIncome.add(specOutpatientDragIncomeCnt);
                allDragIncome.add(allDragIncomeCnt);
            }
        }
        vo.setInpatientDragIncome(inpatientDragIncome);
        vo.setOutpatientDragIncome(outpatientDragIncome);
        vo.setSpecOutpatientDragIncome(specOutpatientDragIncome);
        vo.setAllDragIncome(allDragIncome);
        return Result.success(vo);
    }

    @ApiOperation("查询平均住院天数分布表")
    @PostMapping("/inpatientDayDistributed")
    public Result<InpatientDayDistributedExtendVO> inpatientDayDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        InpatientDayDistributedExtendVO vo = new InpatientDayDistributedExtendVO();
        vo.setDate(date);
        List<Double> inpatientDayAvg = new ArrayList<>();
        List<InpatientDayDistributedVO> datas = medicalServiceDataService.inpatientDayDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(datas == null || datas.isEmpty()) {
                inpatientDayAvg.add(0.0);
            } else {
                double avgDay = 0.0;
                for (InpatientDayDistributedVO inpatientDayDistributedVO : datas) {
                    if (StringUtils.equals(date.get(i), inpatientDayDistributedVO.getDatePoint())) {
                        avgDay = inpatientDayDistributedVO.getInpatientDayAvg();
                    }
                }
                inpatientDayAvg.add(avgDay);
            }
        }
        vo.setInpatientDayAvg(inpatientDayAvg);
        return Result.success(vo);
    }

    @ApiOperation("查询出院人数分布表")
    @PostMapping("/leaveHospitalDistributed")
    public Result<LeaveHospitalPersonDistributedExtendVO> leaveHospitalDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        LeaveHospitalPersonDistributedExtendVO vo = new LeaveHospitalPersonDistributedExtendVO();
        vo.setDate(date);
        List<Integer> leaveHospitalPerson = new ArrayList<>();
        List<LeaveHospitalPersonDistributedVO> datas = medicalServiceDataService.leaveHospitalDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(datas == null || datas.isEmpty()){
                leaveHospitalPerson.add(0);
            } else {
                int leavePerson = 0;
                for (LeaveHospitalPersonDistributedVO leaveHospitalPersonDistributedVO : datas) {
                    if (StringUtils.equals(date.get(i), leaveHospitalPersonDistributedVO.getDatePoint())) {
                        leavePerson = leaveHospitalPersonDistributedVO.getLeaveHospitalPerson();
                    }
                }
                leaveHospitalPerson.add(leavePerson);
            }
        }
        vo.setLeaveHospitalPerson(leaveHospitalPerson);
        return Result.success(vo);
    }

    @ApiOperation("查询门特诊健康检查人数分布表")
    @PostMapping("/allOutpatientAndCheckPersonDistributed")
    public Result<OutpatientAndCheckPersonDistributedExtendVO> allOutpatientAndCheckPersonDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        OutpatientAndCheckPersonDistributedExtendVO vo = new OutpatientAndCheckPersonDistributedExtendVO();
        vo.setDate(date);
        List<Integer> checkPerson = new ArrayList<>();
        List<Integer> allOutpatientPerson = new ArrayList<>();
        List<OutpatientAndCheckPersonDistributedVO> outpatientAndCheckPersonDistributedVOs = medicalServiceDataService.allOutpatientAndCheckPersonDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(outpatientAndCheckPersonDistributedVOs == null || outpatientAndCheckPersonDistributedVOs.isEmpty()){
                allOutpatientPerson.add(0);
                checkPerson.add(0);
            } else {
                int outpatientPerson = 0;
                int checkPersonCnt = 0;
                for (OutpatientAndCheckPersonDistributedVO outpatientAndCheckPersonDistributedVO : outpatientAndCheckPersonDistributedVOs) {
                    if (StringUtils.equals(date.get(i), outpatientAndCheckPersonDistributedVO.getDatePoint())) {
                        outpatientPerson = outpatientAndCheckPersonDistributedVO.getAllOutpatientPerson();
                        checkPersonCnt = outpatientAndCheckPersonDistributedVO.getCheckPerson();
                    }
                }
                allOutpatientPerson.add(outpatientPerson);
                checkPerson.add(checkPersonCnt);
            }
        }
        vo.setAllOutpatientPerson(allOutpatientPerson);
        vo.setCheckPerson(checkPerson);
        return Result.success(vo);
    }

    @ApiOperation("查询收入分布表")
    @PostMapping("/incomeDistributed")
    public Result<IncomeDistributedExtendVO> incomeDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        IncomeDistributedExtendVO vo = new IncomeDistributedExtendVO();
        vo.setDate(date);
        List<Double> totalIncome = new ArrayList<>();
        List<Double> inpatientIncome = new ArrayList<>();
        List<Double> outpatientIncome = new ArrayList<>();
        List<Double> specOutpatientIncome = new ArrayList<>();
        List<IncomeDistributedVO> incomeDistributedVO = medicalServiceDataService.incomeDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(incomeDistributedVO == null || incomeDistributedVO.isEmpty()) {
                totalIncome.add(0.0);
                inpatientIncome.add(0.0);
                outpatientIncome.add(0.0);
                specOutpatientIncome.add(0.0);
            } else {
                double totalIncomeCnt = 0.0;
                double inpatientIncomeCnt = 0.0;
                double outpatientIncomeCnt = 0.0;
                double specOutpatientIncomeCnt = 0.0;
                for (IncomeDistributedVO incomeVo : incomeDistributedVO) {
                    if(StringUtils.equals(date.get(i), incomeVo.getDatePoint())) {
                        totalIncomeCnt = incomeVo.getTotalIncome();
                        inpatientIncomeCnt = incomeVo.getInpatientIncome();
                        outpatientIncomeCnt = incomeVo.getOutpatientIncome();
                        specOutpatientIncomeCnt = incomeVo.getSpecOutpatientIncome();
                    }
                }
                totalIncome.add(totalIncomeCnt);
                inpatientIncome.add(inpatientIncomeCnt);
                outpatientIncome.add(outpatientIncomeCnt);
                specOutpatientIncome.add(specOutpatientIncomeCnt);
            }
        }
        vo.setInpatientIncome(inpatientIncome);
        vo.setTotalIncome(totalIncome);
        vo.setOutpatientIncome(outpatientIncome);
        vo.setSpecOutpatientIncome(specOutpatientIncome);
        return Result.success(vo);
    }

    @ApiOperation("查询门特诊次均费用分布表")
    @PostMapping("/outpatientAvgCostDistributed")
    public Result<OutpatientAvgCostExtendVO> outpatientAvgCostDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        OutpatientAvgCostExtendVO vo = new OutpatientAvgCostExtendVO();
        vo.setDate(date);
        List<Double> avgCost = new ArrayList<>();
        List<OutpatientAvgCostVO> datas = medicalServiceDataService.outpatientAvgCostDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            double avgCostCnt = 0.0;
            for (OutpatientAvgCostVO outpatientAvgCostVO : datas) {
                if(StringUtils.equals(date.get(i), outpatientAvgCostVO.getDatePoint())) {
                    avgCostCnt = outpatientAvgCostVO.getOutpatientAvgCost();
                }
            }
            avgCost.add(avgCostCnt);
        }
        vo.setOutpatientAvgCost(avgCost);
        return Result.success(vo);
    }
}
